<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <script>
        //reduce,没有设置初始值就为第一个元素，之后就为return 的值
    let arr=[1,2,3,4,5];
    //没有设置初始值
    arr.reduce(function(pre,value,index,array){
        console.log(pre,value);
        return 99;//下一次的pre
    });
    //设置初始值
    arr.reduce(function(pre,value,index,array){
        console.log(pre,value);
        return 99;//下一次的pre
    },0);//设置初始值为0


    //计算数组元素的次数
    let array=[1,3,54,1,64,2,1,3,5,56,54];
    function arrcount(arr,number){
       return arr.reduce(function(pre,value){
            pre+=(value==number?1:0);//如果是这个数就加1
            return pre;
        },0);
    }
     console.log(arrcount(array,1));//3
   

     //获取最大值
    // console.log(Math.max(...array));
    function arrMax(arr){
       return arr.reduce(function(pre,value){
            return pre>value?pre:value;
        });
    }
    console.log(arrMax(array));//64

    //总结reduce很好用，自带一些实际很好用的到的方法

    //取超过3的元素，放到数组里去
    let arrays=[1,2,3,4,5];
  let new_arrays=  arrays.reduce(function(arr,value){//初始值为[],那么value就从第一个开始
      if(value>3)arr.push(value);
      return arr;
    },[]);
     console.log(new_arrays);



console.log("________________________");
    //返回超过5000元的学生名字
    let student_money=[{name:"唐玮",money:2000},{name:"华子",money:5200},{name:"彩霞",money:10000}];
    function over_student(array,over_money){
       return array.reduce(function(pre,value){
            if(over_money<=value.money) pre.push(value);
            return pre;
        },[]).map(value => value.name); //只返回超过5000元的学生姓名(这里用到了箭头函数);
    }
    console.table(over_student(student_money,5000));


    //数组去重
    let student_list=[{name:"唐玮",money:2000},{name:"华子",money:5200},{name:"彩霞",money:10000},{name:"唐玮",money:2000},
                  {name:"华子",money:5200} ];


         function arrayfilter(array){
             return array.reduce(function(arr,item){
                let find_boolean=arr.find(function(value){
                    return value.name==item.name;
                });
                if(!find_boolean) arr.push(item);
               return arr;
             },[]);

         }
         console.table(arrayfilter(student_list));
  </script>
</body>
</html>